<template>
  <div ref="selectList" :class="['it-select', className]">
    <span v-if="selectName" class="it-select-input" @click="openSelect">
      {{ selectName }}</span
    >
    <span v-else class="it-select-input gray" @click="openSelect">{{
      defaultSelName
    }}</span>
    <span
      :class="['triangle-down', { rotate: rotate }]"
      @click="openSelect"
    ></span>
    <div
      :class="[
        'it-select-options-panel',
        'scrollbar',
        showOptions ? 'show' : 'hidden',
      ]"
    >
      <p
        v-for="(item, index) in selectData"
        :key="index"
        :class="['it-select-option', selV == item.value ? 'check' : '']"
        :title="item.name"
        @click="getValue(item.name, item.value)"
      >
        {{ item.name }}
      </p>
    </div>
  </div>
</template>
<script setup>
import { computed, ref, watch } from 'vue'
import { onClickOutside } from '@vueuse/core'
const props = defineProps({
  prop: {
    type: String,
    default: '',
  },
  className: {
    type: String,
    default: '',
  },
  selectData: {
    type: Array,
    default: () => {
      return []
    },
  },
  selValue: {
    type: [String, Number],
    default: '',
  },
  placeholder: {
    type: String,
    default: '',
  },
  //选中后是否替换
  replace: {
    type: Boolean,
    default: false,
  },
  // //是否是多选
  // multiple: {
  //   type: Boolean,
  //   default: false,
  // },
  disabled: {
    type: Boolean,
    default: false,
  },
})
let selV = ref(props.selValue)
watch(
  () => props.selValue,
  () => {
    selV.value = props.selValue
  }
)
let rotate = ref(false)
let showOptions = ref(false)
const defaultSelName = ref()
const selectName = computed(() => {
  let selName
  props.selectData.forEach((item) => {
    if (props.replace) {
      if (item.value == selV.value) {
        selName = item.name
      }
    } else {
      if (item.value === '') {
        selName = props.placeholder
      } else {
        defaultSelName.value = props.placeholder
      }
    }
  })
  return selName
})
const disabledStatus = computed(() => {
  return props.disabled
})
const openSelect = () => {
  if (disabledStatus.value) {
    return false
  } else {
    showOptions.value = !showOptions.value
    rotate.value = !rotate.value
  }
}
const emits = defineEmits(['onSelect'])
const getValue = (name, value) => {
  selV.value = value
  emits('onSelect', { prop: props.prop, name, value })
  closeSelect()
}
const closeSelect = () => {
  showOptions.value = false
  rotate.value = false
}

const selectList = ref(null)
onClickOutside(selectList, () => {
  if (showOptions.value) {
    closeSelect()
  }
})
</script>
<style scoped>
.it-select {
  width: 100%;
  height: 100%;
  position: relative;
}
.it-select-input {
  width: calc(100% - 20px);
  height: 100%;
  line-height: 100%;
  text-align: center;
  cursor: pointer;
  border: none;
  padding: 10px;
}
.it-select-input.gray {
  font-size: 18px;
  font-weight: 400;
  line-height: 1;
  color: #a2abc7;
}
.triangle-down {
  width: 10px;
  height: 10px;
  background-image: url(./images/icondown.png);
  background-size: 10px 6px;
  background-position: center center;
  background-repeat: no-repeat;
  position: absolute;
  top: 50%;
  transform: translateY(-50%) rotate(0deg);
  transition: transform 0.3s ease-in-out;
  cursor: pointer;
}
.rotate {
  transform: translateY(-50%) rotate(180deg);
}
.it-select-options-panel {
  position: absolute;
  width: 100%;
  top: calc(100% + 15px);
  left: 0;
  background: #ffffff;
  box-shadow: 0px 0px 15px rgba(32, 110, 242, 0.15);
  border-radius: 10px;
  max-height: 264px;
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 1;
}
.it-select-option {
  padding: 0 22px;
  cursor: pointer;
  height: 44px;
  line-height: 44px;
  font-size: 18px;
  color: #333333;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.it-select-option.check {
  color: #206ef2;
  background: #f5f7fe;
}
.it-select-option:hover {
  background: #f5f7fe;
}
.show {
  display: block;
}
.hidden {
  display: none;
}
</style>
